<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="案例名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入案例名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在地区" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入所在地区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始服务起始年度" label-width="130px" prop="beginYear">
        <el-date-picker
          v-model="beginYear"
          size="small"
          style="width: 240px"
          type="year"
          @change="getbeginYear"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束服务起始年度" label-width="130px" prop="endYear">
        <el-date-picker
          v-model="endYear"
          size="small"
          style="width: 240px"
          type="year"
          @change="getendYear"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      stripe
      v-loading="loading"
      :data="caseList"
      @sort-change="tableSortChange"
      :default-sort="{prop: 'year', order: 'descending'}"
    >
      <el-table-column
        label="案例名称"
        align="center"
        prop="title"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="所在地区"
        align="center"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        prop="province"
      >
        <template slot-scope="scope">{{ scope.row.province }}{{ scope.row.city }}</template>
      </el-table-column>
      <el-table-column
        label="服务起始年度"
        align="center"
        prop="year"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改典型案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row type="flex" justify="left">
          <el-col :span="24">
            <div class="grid-content bg-purple-dark">
              <el-form-item label="案例名称:">{{ form.title }}</el-form-item>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="案例所在地区:">{{ form.province }}{{ form.city }}</el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务起始年度:">{{ form.year }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="案例概要说明:">{{ form.content }}</el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.attachments">
          <el-form-item label="附件:">
            <el-row v-for="attachment in form.attachments" :key="attachment.fileName">
              <el-col :span="24">
                <span>{{ attachment.fileName }}</span>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-search"
                  @click="handlePreview(attachment)"
                >预览</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注:">{{ form.remark }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCase,
  getCase,
  delCase,
  addCase,
  updateCase,
  exportCase
} from "@/api/businessStandardLibrary/case/case";
import aTitle from "@/components/Title";
export default {
  name: "Case",
  components: { aTitle },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      beginYear: undefined,
      endYear: undefined,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 典型案例表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        province: undefined,
        city: undefined,
        year: "",
        businessCategoryId: undefined,
        beginYear: "",
        endYear: "",
        sortBy: undefined,
        order: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        guid: [{ required: true, message: "guid不能为空", trigger: "blur" }]
      }
    };
  },
  created () {
    this.queryParams.businessCategoryId = this.$route.params.businessCategoryId;
    this.getList();
  },
  mounted(){
    document.title=document.getElementById("businessNameTitle").innerHTML;
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务主页/典型案例.xlsx`
      )}&name=${encodeURIComponent("典型案例.xlsx")}`;
    },
    getendYear (val) {
      this.queryParams.endYear = new Date(val).getFullYear()
    },
    getbeginYear (val) {
      this.queryParams.starYear = new Date(val).getFullYear()
    },
    /** 查询典型案例列表 */
    getList () {
      this.loading = true;
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        businessCategoryId: undefined,
        title: undefined,
        province: undefined,
        city: undefined,
        serviceYear: undefined,
        year: undefined,
        content: undefined,
        remark: undefined,
        attachments: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.beginYear = undefined
      this.endYear = undefined
      this.queryParams.starYear = undefined
      this.queryParams.endYear = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.guid;
      getCase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "案例详情";
      });
    },
    handlePreview (row) {
      this.filePreview(row.fileName, row.storagePath);
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;

      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
};
</script>
